﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities.Configuration;
using Viettel.VOFFICE.DAO.Models;
using Viettel.VOFFICE.Web.Helpers;
using Viettel.VOFFICE.Web.Models;
using Viettel.VOFFICE.Resources;
namespace Viettel.VOFFICE.Web.Controllers
{
    public class SystemMenuController : BaseController
    {
        #region Build System menu
        [HttpPost]
        [ActionName("get_system_menu")]
        public JsonResult GetMenus()
        {
            try
            {
                var lstMenu = unitOfWork.SystemMenuRepository.GetRootMenu();
                var rootNode = new SystemMenu()
                                   {
                                       attr = new SystemMenuAttr(),
                                       id = 0,
                                       data = Resources.Resources.ManageSystemMenu_RootNodeMenu
                                   };
                rootNode.attr.id = 0;
                rootNode.attr.title = Resources.Resources.ManageSystemMenu_RootNodeMenu;
                foreach (var menu in lstMenu)
                {
                    var itemNode = new SystemMenu()
                                       {
                                           attr = new SystemMenuAttr(),
                                           id = menu.id,
                                           data = menu.Name
                                       };
                    itemNode.attr.id = menu.id;
                    itemNode.attr.title = menu.Name;
                    PopulateTree(menu.id, itemNode);
                    rootNode.children.Add(itemNode);
                }
                return Json(rootNode);
            }
            catch (Exception)
            {
                return Json(SimplyResponse.Response500());
            }
        }
        private void PopulateTree(int rootPath, SystemMenu rootNode)
        {
            foreach (var item in unitOfWork.SystemMenuRepository.GetByParentId(rootPath))
            {
                var newRoot = new SystemMenu { attr = new SystemMenuAttr(), data = item.Name };
                newRoot.attr.id = item.id;
                newRoot.attr.title = item.DisplayName;
                PopulateTree(item.id, newRoot);
                rootNode.children.Add(newRoot);
            }
        }
        #endregion

        
        [HttpGet]
        [ActionName("get_menu")]
        public JsonResult GetMenuItem(int id)
        {
            try
            {
                var menuItem = unitOfWork.SystemMenuRepository.GetById(id);
                if(menuItem!=null)
                {
                    var returnMenu = new SystemMenuModel
                                         {
                                             id=menuItem.id,
                                             Name = menuItem.Name,
                                             Description = menuItem.Description,
                                             NavigatorUrl = menuItem.NavigatorUrl,
                                             IsShow = menuItem.IsShow != null && (bool) menuItem.IsShow,
                                             IsActive = menuItem.IsActive != null && (bool) menuItem.IsActive
                                         };
                    return Json(new JsonResponse
                                    {
                                        Status = true,
                                        Result = returnMenu
                                    }, JsonRequestBehavior.AllowGet);
                }
                return Json(SimplyResponse.Response200());
            }
            catch (Exception)
            {
                return Json(SimplyResponse.Response500());
            }
        }
        [HttpPost]
        [ActionName("move_menu")]
        public JsonResult MoveMenu(int curentid, int? parentid)
        {
            var curentMenu = unitOfWork.SystemMenuRepository.GetById(curentid);
            if (curentMenu != null)
            {
                curentMenu.ParentId = parentid;
                unitOfWork.SystemMenuRepository.Update(curentMenu);
                return Json(SimplyResponse.Response200());
            }
            return Json(SimplyResponse.Response500());
        }
        [HttpPost]
        [ActionName("add_menu")]
        public JsonResult AddMenu(SystemMenuModel model)
        {
            try
            {
                List<ValidationResult> outError;
                if (!model.IsValid(out outError))
                {
                    return Json(new JsonResponse()
                    {
                        Status = false,
                        Result = string.Join(", ", outError.Select(o => o.ErrorMessage).ToArray())
                    });
                }
                var systemMemu = new v_SystemMenu
                                     {
                                         Name = model.Name,
                                         Description = model.Description,
                                         IsActive = model.IsActive,
                                         IsShow = model.IsShow,
                                         NavigatorUrl = model.NavigatorUrl,
                                         ParentId = model.ParentId
                                     };
                unitOfWork.SystemMenuRepository.Insert(systemMemu);
                return Json(SimplyResponse.ResposeWithId(systemMemu.id));
            }
            catch (Exception exception)
            {
                return Json(SimplyResponse.Response500(exception.ToString()));
            }
        }
        [HttpPost]
        [ActionName("delete_menu")]
        public JsonResult DeleteMenu(int curentid)
        {
            var children = unitOfWork.SystemMenuRepository.GetByParentId(curentid);
            if (children == null)
            {
                var curentMenu = unitOfWork.SystemMenuRepository.GetById(curentid);
                if (curentMenu != null)
                {
                    unitOfWork.SystemMenuRepository.Delete(curentMenu);
                    return Json(SimplyResponse.Response200());
                }
            }

            return Json(SimplyResponse.Response500(Resources.Resources.SystemMenu_RecursiveData));
        }
        [HttpPut]
        [ActionName("update_menu")]
        public JsonResult UpdateMenu(SystemMenuModel model)
        {
            try
            {
                var systemModel = unitOfWork.SystemMenuRepository.GetById(model.id);
                if (systemModel != null)
                {
                    systemModel.IsActive = model.IsActive;
                    systemModel.IsShow = model.IsShow;
                    systemModel.Name = model.Name;
                    systemModel.Description = model.Description;
                    systemModel.NavigatorUrl = model.NavigatorUrl;
                    unitOfWork.SystemMenuRepository.Update(systemModel);
                }
                return Json(SimplyResponse.Response200());
            }
            catch (Exception)
            {
                return Json(SimplyResponse.Response500());
            }
        }
        
    }
}
